/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

/**
 * <p>Project: jse2204 - Arr5
 * <p>Powered by webrx On 2022-09-06 14:16:21
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Arr5 {
    public static void main(String[] args) {
        Double[] ds = new Double[10];
        for (int i = 0; i < ds.length; i++) {
            ds[i] = Double.parseDouble(String.format("%.2f", Math.random()));
        }

        System.out.println("排序前：" + Arrays.toString(ds));
        //int index = 0;
        //for (double d : ds) {
        //    System.out.printf("ds[%d] = %.2f ", index++, d);
        //}

        //排升序从小到大
        Arrays.sort(ds);
        System.out.println("排序前：" + Arrays.toString(ds));
        Arrays.sort(ds, (a, b) -> a.compareTo(b));
        System.out.println("排序前：" + Arrays.toString(ds));

        Random rand = new Random();
        int index = 0;
        while (true) {
            int a = rand.nextInt(ds.length);
            int b = rand.nextInt(ds.length);
            if (a == b) continue;
            double t = ds[a];
            ds[a] = ds[b];
            ds[b] = t;
            if (++index >= 100) break;
        }
        System.out.println("乱序：" + Arrays.toString(ds));

        //打降序从大到小
        Arrays.sort(ds, (x, y) -> y.compareTo(x));
        System.out.println("排降序：" + Arrays.toString(ds));
        Arrays.sort(ds, Comparator.reverseOrder());
        System.out.println("排降序：" + Arrays.toString(ds));

        //洗牌 乱序
        Arrays.sort(ds, (a,b) -> Math.random() > .5 ? 1 : -1);
        Arrays.sort(ds, (a,b) -> Math.random() > .5 ? 1 : -1);
        Arrays.sort(ds, (a,b) -> Math.random() > .5 ? 1 : -1);
        Arrays.sort(ds, (a,b) -> Math.random() > .5 ? 1 : -1);
        System.out.println("乱序：" + Arrays.toString(ds));



    }
}
